﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;

namespace Summerset.Isbndb.Categories
{
    
    /// <summary>
    /// Represents information about a category returned from the categories collection.
    /// </summary>
    public sealed class CategoryElement : ResponseElementBase
    {
        #region Constructor + Overridden Properties

        /* Element Example:
            <CategoryData category_id="science.mathematics" parent_id="science">
                <Name>Mathematics</Name>
            </CategoryData>
         */

        /// <summary>
        /// Initializes a new instance of the <see cref="CategoryElement"/> class.
        /// </summary>
        /// <param name="element">The xml element containing the underlying information for this instance.</param>
        public CategoryElement(XmlElement element) 
            : base(element) { }

        /// <summary>
        /// Gets the element name that is expected by the constructor of this object.
        /// If the provided element name does not match the expected element name, an exception will be thrown.
        /// </summary>
        protected override string ExpectedElementName
        {
            get { return "CategoryData"; }
        }

        #endregion
        #region Common Response Properties

        /// <summary>
        /// Gets the ISBNdb-specific id that uniquely-identifies this category.
        /// </summary>
        public string CategoryId
        {
            get { return _element.Attributes["category_id"].Value; }
        }

        /// <summary>
        /// The ISBNdb-specific id of the parent category.
        /// </summary>
        public string ParentId
        {
            get { return _element.Attributes["parent_id"].Value; }
        }

        /// <summary>
        /// Gets the friendly name of this category.  This value can be displayed to users.
        /// </summary>
        public string Name
        {
            get { return ((XmlElement)_element.SelectSingleNode("/Name")).InnerText; }
        }

        #endregion
        #region Additional Response Properties

        /// <summary>
        /// OPTIONAL RESULT: Gets additional details about this category.
        /// This must be explicitly requested from the api by providing the 'details' argument to the request.
        /// </summary>
        public CategoryElementDetail Details
        {
            get
            {
                if (_details == null)
                {
                    var detailElement = _element.SelectSingleNode("/Details");

                    if (detailElement != null)
                        _details = new CategoryElementDetail((XmlElement)detailElement);
                }

                return _details;
            }
        }
        CategoryElementDetail _details;

        /// <summary>
        /// OPTIONAL RESULT: Gets the sub categories of this category.
        /// This must be explicitly requested from the api by providing the 'subcategories' argument to the request.
        /// </summary>
        public ReadOnlyCollection<CategoryElementSubCategory> SubCategories
        {
            get
            {
                if (_subcategories == null)
                    _subcategories = base.BuildChildCollection<CategoryElementSubCategory>("/SubCategories/SubCategory");

                return _subcategories;
            }
        }
        ReadOnlyCollection<CategoryElementSubCategory> _subcategories;

        #endregion
    }
}
